package cn.shequren.communityPeople.startup.ui;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import java.util.List;


public class ViewPagerAdapter extends PagerAdapter {

    List<ImageView> viewsList;
    PagerOnclick pagerOnclick;

    public ViewPagerAdapter(List<ImageView> viewsList, PagerOnclick pagerOnclick) {
        this.viewsList = viewsList;
        this.pagerOnclick = pagerOnclick;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ImageView view = viewsList.get(position % viewsList.size());
        ((ViewPager) container).removeView(view);
    }

    @Override
    public Object instantiateItem(View container, final int position) {
        ((ViewPager) container).addView(viewsList.get(position));
        View view = viewsList.get(position);
        view.setOnClickListener(arg0 -> pagerOnclick.onClickPager(position, view));
        return viewsList.get(position);
    }

    @Override
    public int getCount() {
        return viewsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public interface PagerOnclick {
        void onClickPager(int position, View view);
    }
}
